package org.farm2.files.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.farm2.base.domain.FarmUserContext;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.files.domain.ResourceFile;
import org.farm2.files.dto.ResourceFileDto;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.files.utils.FileDownloadUtils;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.caches.FarmCacheKeys;
import org.farm2.tools.caches.FarmCaches;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataQueryDto;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.db.commons.DBSort;
import org.farm2.tools.db.commons.FarmUUID;
import org.farm2.tools.web.FarmResponseCode;
import org.farm2.tools.web.FarmResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static org.farm2.base.domain.FarmUserContextLoader.getCurrentUser;

/**
 * 附件上传下载专用
 *
 * @author cbtg自动生成  2025-1-13 14:50:11
 */
@RestController
@RequestMapping("/api/files")
public class FileAccessController {
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;

    @Autowired
    private FileDownloadUtils fileDownloadUtils;

    @PreAuthorize("@farmAction.has('files.upload')")
    @PostMapping("/upload")
    public FarmResponseResult uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return FarmResponseResult.getInstance(FarmResponseCode.ERROR, "请上传文件", null);
        }
        try {
            resourceFileServiceImpl.validateFileByUpload(file, true);
            ResourceFile rfile = resourceFileServiceImpl.saveFile(file.getOriginalFilename(), file);
            return FarmResponseResult.getInstance(FarmResponseCode.SUCESS, rfile);
        } catch (Exception e) {
            e.printStackTrace();
            return FarmResponseResult.getInstance(FarmResponseCode.ERROR, e.getMessage(), null);
        }
    }


    @GetMapping("/download/{id}")
    public void downloadFile(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResourceFile rfile = resourceFileServiceImpl.getResourceFileById(id);
        File file = resourceFileServiceImpl.getFile(rfile);
        FarmUserContext userContext = FarmUserContextLoader.getCurrentUser();

        if (rfile.getFilesize() > 104857600) {
            fileDownloadUtils.rangeStreamDownloadFile(request, response, file, rfile.getTitle(), "application/octet-stream");
        } else {
            fileDownloadUtils.simpleDownloadFile(file, rfile.getTitle(), "application/octet-stream", response, false);
        }
    }
}
